﻿/*******************************************************************************
* Copyright (C) JuCheap.Com
* 
* Author: dj.wong
* Create Date: 2015/8/21
* Description: Automated building by service@jucheap.com 
* 
* Revision History:
* Date         Author               Description
*
*********************************************************************************/

using JuCheap.Core.Data.Configurations;
using JuCheap.Core.Data.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Reflection;

namespace JuCheap.Core.Data
{
    /// <summary>
    /// JuCheapContext
    /// </summary>
    public class JuCheapContext : DbContext
    {

        /// <summary>
        /// JuCheapContext
        /// </summary>
        public JuCheapContext(DbContextOptions<JuCheapContext> options):
            base(options)
        {
            
        }

        /// <summary>
        /// OnModelCreating
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            
            //添加FluentAPI配置
            var typesToRegister = typeof(SystemConfigConfiguration).Assembly.GetTypes()
                .Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null && !q.FullName.StartsWith("JuCheap.Core.Data.Configurations.BaseConfiguration"));

            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.ApplyConfiguration(configurationInstance);
            }

            modelBuilder.Entity<Worker>()
                .HasOne(w => w.Company)
                .WithMany()
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<Worker>()
                .HasOne(w => w.JobPosition)
                .WithMany()
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<PaiBan>()
                .HasOne(p => p.BanCi)
                .WithMany()
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<PaiBan>()
                .HasOne(p => p.Company)
                .WithMany()
                .OnDelete(DeleteBehavior.SetNull);
            modelBuilder.Entity<PaiBan>()
                .HasOne(p => p.Worker)
                .WithMany()
                .OnDelete(DeleteBehavior.SetNull);
            modelBuilder.Entity<Company>()
                .HasOne(c => c.Organization)
                .WithMany()
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<CheckWorkAttendance>()
                .HasOne(c => c.Worker)
                .WithMany()
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<JobPosition>().HasOne(c => c.JobPositionType)
                .WithMany()
                .OnDelete(DeleteBehavior.Cascade);


            //modelBuilder.Entity<JobPositionType>().HasOne(c => c.Name)
            //  .WithMany()
            //  .OnDelete(DeleteBehavior.Cascade);

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

        #region DbSets

        /// <summary>
        /// 用户
        /// </summary>
        public DbSet<UserEntity> Users { get; set; }
        public DbSet<Worker> Workers { get; set; }
        /// <summary>
        /// 角色
        /// </summary>
        public DbSet<RoleEntity> Roles { get; set; }

        /// <summary>
        /// 菜单
        /// </summary>
        public DbSet<MenuEntity> Menus { get; set; }

        /// <summary>
        /// 用户角色关系
        /// </summary>
        public DbSet<UserRoleEntity> UserRoles { get; set; }

        /// <summary>
        /// 角色菜单关系
        /// </summary>
        public DbSet<RoleMenuEntity> RoleMenus { get; set; }

        /// <summary>
        /// 路径码
        /// </summary>
        public DbSet<PathCodeEntity> PathCodes { get; set; }

        /// <summary>
        /// 页面访问记录
        /// </summary>
        public DbSet<PageViewEntity> PageViews { get; set; }

        /// <summary>
        /// 登录日志
        /// </summary>
        public DbSet<LoginLogEntity> LoginLogs { get; set; }

        /// <summary>
        /// 系统配置
        /// </summary>
        public DbSet<SystemConfigEntity> SystemConfigs { get; set; } 

        /// <summary>
        /// 区域
        /// </summary>
        public DbSet<AreaEntity> Areas { get; set; }

        /// <summary>
        /// 部门
        /// </summary>
        public DbSet<DepartmentEntity> Departments { get; set; }

        /// <summary>
        /// 访问数据统计
        /// </summary>
        public DbSet<SiteViewEntity> SiteViews { get; set; }

        /// <summary>
        /// 站内信
        /// </summary>
        public DbSet<MessageEntity> Messages { get; set; }

        /// <summary>
        /// 站内信接收人
        /// </summary>
        public DbSet<MessageReceiverEntity> MessageReceivers { get; set; }

        /// <summary>
        /// 班次
        /// </summary>
        public DbSet<BanCi> BanCis { get; set; }
        
        /// <summary>
        /// 公司
        /// </summary>
        public DbSet<Company> Companies { get; set; }

        /// <summary>
        /// 群组
        /// </summary>
        public DbSet<DepartmentGroup> DepartmentGroups { get; set; }
        public DbSet<DepartmentGroupWorker> DepartmentGroupWorkers { get; set; }
        public DbSet<DepartmentGroupDevice> DepartmentGroupDevices { get; set; }

        /// <summary>
        /// 设备
        /// </summary>
        public DbSet<HardWareDevice> HardWareDevices { get; set; }

        /// <summary>
        /// 岗位
        /// </summary>
        public DbSet<JobPosition> JobPositions { get; set; }

        /// <summary>
        /// 岗位类型
        /// </summary>
        public DbSet<JobPositionType> JobPositionTypes { get; set; }

        /// <summary>
        /// 组织
        /// </summary>
        public DbSet<Organization> Organizations { get; set; }

        /// <summary>
        /// 排班
        /// </summary>
        public DbSet<PaiBan> PaiBans { get; set; }

        /// <summary>
        /// 屏幕内容
        /// </summary>
        public DbSet<ScreenContentEntity> ScreenContentEntities { get; set; }

        /// <summary>
        /// 时间段
        /// </summary>
        public DbSet<TimePeriod> TimePeriods { get; set; }

        /// <summary>
        /// 员工考勤
        /// </summary>
        public DbSet<CheckWorkAttendance> CheckWorkAttendances { get; set; }
        /// <summary>
        /// 显示推送
        /// </summary>
        public DbSet<ShowPushEntity> ShowPush { get; set; }

        #endregion
    }
}
